//
//  SectionTableController.m
//  KingMall
//
//  Created by LiuWenyang on 15/7/29.
//  Copyright (c) 2015年 KingVision. All rights reserved.
//

#import "SectionTableController.h"

@implementation SectionTableController


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self.classForCell) forIndexPath:indexPath];
//    if (cell == nil) {
//        cell = [[self.classForCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//        cell.delegate = self;
//    }
    cell.delegate = self;
    id data = [[self.listArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell setValue:data];
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCell:tableViewCell:)]) {
        TableViewCell *tableViewCell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [self.delegate didSelectedCell:self tableViewCell:tableViewCell];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCell:dataSrc:row:)]) {
        id data = [[self.listArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        [self.delegate didSelectedCell:self dataSrc:data row:indexPath.row];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id data = [[self.listArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return [self.classForCell heightForTableViewCell:data];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ((NSMutableArray *)[self.listArray objectAtIndex:section]).count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_sectionIndexTitles objectAtIndex:section];
}

@end
